package com.jgads.ad.sdk;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTFeedAd;
import com.bytedance.sdk.openadsdk.TTNativeAd;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationViewBinder;
import com.jgads.ad.config.TTAdManagerHolder;
import com.jgads.ad.constant.AdConstant;
import com.jgads.ad.sdk.mediation.utils.ViewUtil;
import com.jgads.ad.utils.TToast;
import com.jgads.ad.utils.UIUtils;
import com.junion.JgAds;

import java.util.ArrayList;
import java.util.List;

/**
 * @author maipian
 * @description 描述
 * @date 9/27/23
 */
public class NativeActivity extends Activity {
    private TTAdNative mTTAdNative;

    private ConstraintLayout rlAdContainer;
    private FrameLayout flMaterialContainer;
    private TextView tvTitle;
    private TextView tvDesc;
    private TextView tvAdTarget;
    private ImageView ivClose;

    private Context mContext;

    private Button mButtonLoadAd;

    private TTFeedAd mTTAd;

    @SuppressWarnings("RedundantCast")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_native);
        mContext = this.getApplicationContext();
        mButtonLoadAd = (Button) findViewById(R.id.btn_express_load);
        mButtonLoadAd.setOnClickListener(mClickListener);

        rlAdContainer = findViewById(R.id.rlAdContainer);
        flMaterialContainer = findViewById(R.id.flMaterialContainer);
        tvTitle = findViewById(R.id.tvTitle);
        tvDesc = findViewById(R.id.tvDesc);
        tvAdTarget = findViewById(R.id.tvAdTarget);
        ivClose = findViewById(R.id.ivClose);

        mTTAdNative = TTAdManagerHolder.get().createAdNative(this);
        //step3:(可选，强烈建议在合适的时机调用):申请部分权限，如read_phone_state,防止获取不了imei时候，下载类广告没有填充的问题。
        TTAdManagerHolder.get().requestPermissionIfNecessary(this);
        Button button = (Button)findViewById(R.id.btn_ane_back);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @SuppressWarnings("EmptyMethod")
    @Override
    protected void onResume() {
        super.onResume();
    }

    private final View.OnClickListener mClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.btn_express_load) {
                loadExpressAd(AdConstant.FEED_NATIVE);
            }
        }
    };

    private void loadExpressAd(String codeId) {
        float expressViewWidth = UIUtils.getScreenWidthInPx(this);
        float expressViewHeight = 0;

        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(codeId) //广告位id
                .setAdCount(1) //请求广告数量为1到3条
                .setExpressViewAcceptedSize(expressViewWidth,expressViewHeight) //期望模板广告view的size,单位dp
                .build();
        //step5:请求广告，对请求回调的广告作渲染处理

        mTTAdNative.loadFeedAd(adSlot, new TTAdNative.FeedAdListener() {
            @Override
            public void onError(int code, String message) {
                TToast.show(NativeActivity.this, "load error : " + code + ", " + message);
            }

            @Override
            public void onFeedAdLoad(List<TTFeedAd> list) {
                if (list == null || list.size() == 0){
                    return;
                }
                rlAdContainer.setVisibility(View.VISIBLE);
                mTTAd = list.get(0);
                bindAdListener(mTTAd);
            }
        });
    }
    private void bindAdListener(TTFeedAd ad) {
        if (ad != null) {
            if (ad.getImageMode() == TTAdConstant.IMAGE_MODE_VIDEO || ad.getImageMode() == TTAdConstant.IMAGE_MODE_VIDEO_VERTICAL || ad.getImageMode() == TTAdConstant.IMAGE_MODE_LIVE) {
            } else {
                ImageView imageView = new ImageView(flMaterialContainer.getContext());
                ViewGroup.LayoutParams imageViewLayoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
                JgAds.getInstance().getImageLoader().loadImage(imageView.getContext(), ad.getImageList().get(0).getImageUrl(), imageView);
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageView.setLayoutParams(imageViewLayoutParams);
                ViewUtil.addAdViewToAdContainer(flMaterialContainer, imageView);
            }
            if (tvTitle != null) {
                tvTitle.setText(ad.getTitle());
            }
            if (tvDesc != null) {
                tvDesc.setText(ad.getDescription());
            }
            if (tvAdTarget != null) {
                tvAdTarget.setText("广告");
            }

            List<View> clickView = new ArrayList<>();
            clickView.add(rlAdContainer);
            clickView.add(flMaterialContainer);

            MediationViewBinder viewBinder = new MediationViewBinder.Builder(R.layout.activity_native)
                    .titleId(R.id.tvTitle)
                    .descriptionTextId(R.id.tvDesc)
                    .mediaViewIdId(R.id.flMaterialContainer)
                    .build();

            ad.registerViewForInteraction(this, rlAdContainer, clickView, null, null, new TTNativeAd.AdInteractionListener() {
                @Override
                public void onAdClicked(View view, TTNativeAd ttNativeAd) {

                }

                @Override
                public void onAdCreativeClick(View view, TTNativeAd ttNativeAd) {

                }

                @Override
                public void onAdShow(TTNativeAd ttNativeAd) {

                }
            }, viewBinder);
            ivClose.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    rlAdContainer.setVisibility(View.GONE);
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mTTAd != null) {
            mTTAd.destroy();
        }
    }
}
